/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.cambios;

import com.mapping.CambioComp;
import com.mapping.Componentes;
import com.mapping.Ticket;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
/**
 *
 * @author Andres
 */
@Namespace(value = "/")
@Action(value = "mostrarCambioComponente", results = {@Result(name = "success", location = "/GestionCambios/mostrarCambioComponente.jsp")})
public class MostrarCambioComponenteAction extends ActionSupport{
    private String idTicket;
    private String idComponente;
    private List<Componentes> listaComponentes;
    private CambioComp cc;
    @Override
    public String execute(){
        String consulta1 = "select IDTICKET, IDCOMPONENTE, IDCOMPONENTECAMBIO from cambio_comp "
                            + "where idTicket=?1 and idComponente=?2";
        String consulta2 = "select ID,IDTIPO,IDDISPORIGEN,IDDISPACTUAL,CARACTERISTICAS,STATUS from componentes where idDispActual is null";
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
            Query query =  em.createNativeQuery(consulta1,CambioComp.class);
            query.setParameter(1,idTicket);
            query.setParameter(2,idComponente);
            cc =  (CambioComp)query.getSingleResult();
            query =  em.createNativeQuery(consulta2,Componentes.class);
            listaComponentes = (List<Componentes>) query.getResultList();
        tx.commit();
        em.close();
        return SUCCESS;
    }

    public void setIdTicket(String idTicket) {
        this.idTicket = idTicket;
    }

    public void setIdComponente(String idComponente) {
        this.idComponente = idComponente;
    }

    public List<Componentes> getListaComponentes() {
        return listaComponentes;
    }

    public CambioComp getCc() {
        return cc;
    }
    
}
